<%@ page language="java" contentType="text/html;charset=UTF-8" pageEncoding="UTF-8"%>
<%@ taglib uri="/struts-tags" prefix="s"%>
<%
	String path = request.getContextPath();
	String basePath = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort() + path + "/";
%>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>组织机构管理</title>
<link type="text/css" rel="stylesheet" href="../<s:property value="#session.themeStyle.mainCss" default="Skin/PM/LightBlue/main.css"/>" />
<link rel="stylesheet" type="text/css" href="<%=basePath%>css/zTreeStyle/zTreeStyle.css" />
<style type="text/css">
</style>
</head>
<body>
	<s:form theme="simple">
		<div class="tabBlock">
			<table style="width: 100%;border-collapse:collapse;border-width: 0px;">
				<tr>
					<td id="tdtree" style="width: 220px;height: 100%;vertical-align: top;">
						<div id="treeDemo">
							<ul id="ultree" class="ztree"></ul>
						</div>
					</td>
					<td style="border-left: 1px #E9EDEE solid;vertical-align: top;">
						<div style="width: 100%; " id="showList">
							&nbsp;&nbsp;<STRONG>当前组织机构：</STRONG>
							<s:textfield name="parent.name" readonly="true" cssClass="inputText" onfocus="showOrgTree()" onclick="showOrgTree()"></s:textfield>
							<s:hidden id="parentId" name="parent.pkId"></s:hidden>
							<s:if test="parent.orgId==0">
								<a class="but28" onclick="addOrg()" style="text-decoration:none;margin-bottom: 10px;">新增机构</a>
							</s:if>
							<s:else>
								<a class="but28" onclick="addDept()" style="text-decoration:none;">新增部门</a>
							</s:else>
							
							<div class="dataGrid" style="margin-top: 10px;">
								<table>
									<tr>
										<th style="width: 5%">序号</th>
										<th style="width: 10%">编号</th>
										<th style="width: 30%">名称</th>
										<th style="width: 10%">使用文档</th>
										<th>操作</th>
									</tr>
									<s:if test="deptInfolist.size==0">
										<tr class="trStyle">
											<td colspan="5">(无)</td>
										</tr>
									</s:if>
									<s:iterator value="deptInfolist" status="st">
										<tr class="trStyle">
											<td><s:property value="#st.index+1"/></td>
											<td>${deptNo}&nbsp;</td>
											<td>${name}&nbsp;</td>
											<td><s:if test="type==1">是</s:if> <s:else>否</s:else></td>
											<td>
												<a style="color: #e9621e;text-decoration: none;cursor: pointer;" onclick="manage(${pkId})">管理</a>
												<a style="color: #e9621e;text-decoration: none;cursor: pointer;" onclick="updateDeptInfo(${pkId})">修改</a>
											</td>
										</tr>
									</s:iterator>
								</table>
							</div>
						</div>
					</td>
				</tr>
			</table>
		</div>
		<input type="hidden" id="deptId" value="<s:property value="deptId"/>" />
		<input type="hidden" id="state" value="<s:property value="state"/>" />
	</s:form>
	<script type="text/javascript" src="<%=basePath%>js/jquery.js"></script>
	<script type="text/javascript" src="<%=basePath%>js/ztree/jquery.ztree.core-3.5.js"></script>
	<script type="text/javascript" src="<%=basePath%>js/modalWindow.js"></script>
	<script type="text/javascript">
		
		var currentDeptId;
		
		var setting = {
			async:{	
				enable: true,	
				url:"orgTree-getNode.action",	
				autoParam:["id"],	
				type: "post"	
			},				
			data: {
				simpleData: {
					enable: true
				}
			},
			callback: {
				onClick: onClick
			}
		};

		$(document).ready(function(){
			initWindow();
		});
		
		/*
		 * 显示组织机构树 SelectTree
		 */
		function showOrgTree(){
			$("#tdtree").show();
			var treeObj =$.fn.zTree.init($("#ultree"), setting, null);
			var currentNodeId = $("#deptId").val();
			if (currentNodeId=="")currentNodeId="0";
			var node = treeObj.getNodeByParam("id", currentNodeId);
			treeObj.selectNode(node);
		}
		
		function onClick(event, treeId, treeNode, clickFlag) {
			var id = treeNode.id;
			var html = "";
			html += "<iframe id=\"deptFrame\" name=\"deptFrame\" src=\"org-manage.action?deptId="+id+"\" frameborder=\"0\" scrolling=\"yes\" style=\"width:100%;\"  onLoad=\"this.height=this.contentWindow.document.documentElement.scrollHeight\" ></iframe>";
			html += "<iframe id=\"deptFrame\" name=\"deptFrame\" src=\"../sys/userInfo-manage.action?deptId="+id+"\"frameborder=\"0\" scrolling=\"yes\" style=\"width:100%;\"   onLoad=\"this.height=this.contentWindow.document.documentElement.scrollHeight\"></iframe>";
			$("#showList").html(html); 			
			if(id==0){
				//获取所有的机构列表
			}else{
				//获取当前机构下的所有部门列表
			}
		}
		
		function initWindow(){
			var height = window.parent.document.getElementById("mainFrame").height-18;
		}
		
		function initFrame(frame){
			var height =frame.contentWindow.document.body.scrollHeight;
			$(frame).height(height);
		}
		
		function manage(id){
			$("#parentId").val(id);
			$("form")[0].submit();
		}
		
		function addOrg() {
			var deptId = $("#deptId").val();
			var state = $("#state").val();
			var type = "";
			var name = "";
			//新增机构
			var url = "initAddOrgInfo.action?orgId=0";
			showModalWindow(
					url,
					window,
					'resizable:no;scroll:yes;status:no;dialogWidth=350px;dialogHeight=176px;center=yes;help=no',
					saveOrgInfo
			);

//			var result=window.showModalDialog(url,'','resizable:no;scroll:yes;status:no;dialogWidth=350px;dialogHeight=176px;center=yes;help=no');
		}
		
		function saveOrgInfo(result){
			var state = $("#state").val();
		   	if(result!=null&&result!='undefined'&&result!=''){
		   		var fromFile = createFrom("addOrgInfo.action");
		   		loadFrom("name",result,fromFile);
		   		loadFrom("state",state,fromFile);
				fromFile.submit();
		   		//window.parent.location.href="addOrgInfo?type="+type+"&name="+name;
			}
		}
		
		function updateDeptInfo(id) {
			currentDeptId = id;
				var url = "<%=basePath%>orginfo/getDeptInfoById.action?deptId="+ id + "&newUrl=" + new Date();
			//				var result=window.showModalDialog(url,'','resizable:no;scroll:yes;status:no;dialogWidth=400px;dialogHeight=240px;center=yes;help=no');
			showModalWindow(
					url,
					window,
					'resizable:no;scroll:yes;status:no;dialogWidth=400px;dialogHeight=240px;center=yes;help=no',
					saveDeptInfo);
		}

		function saveDeptInfo(result) {
			if (result != null && result != 'undefined' && result != '') {
				var datas = eval("(" + result + ")");
				var state = $("#state").val();
				var from = createFrom("updateDeptInfo.action");
				loadFrom("deptId", currentDeptId, from);
				loadFrom("deptNo", datas.deptNo, from);
				loadFrom("name", datas.name, from);
				loadFrom("type", datas.type, from);
				loadFrom("state", state, from)
				from.submit();
			}
		}

		//配置from信息
		function createFrom(actionName) {
			var fromFile = document.createElement("FORM");
			document.body.appendChild(fromFile);
			fromFile.method = "POST";
			fromFile.action = actionName;
			fromFile.style.display = "none";
			return fromFile;
		}

		//注入到from数据中
		function loadFrom(name, value, fromFile) {
			var input = document.createElement("INPUT");
			input.type = "hidden";
			input.name = name;
			input.id = name;
			input.value = value;
			fromFile.appendChild(input);
		}
	</script>
</body>
</html>